package com.example.springbootjpa.repositorie;

import com.example.springbootjpa.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface SysUserRepository extends JpaRepository<SysUser, Long>, JpaSpecificationExecutor<SysUser> {

    @Query(value = "select user_id,login_name,email,avatar,del_flag,login_date from sys_user order by create_time desc limit ?1,?2", nativeQuery = true)
    List<Map<String, Object>> findUserWhere(Integer pageIndex, Integer pageSize);

    @Transactional
    @Modifying
    @Query(value = "delete from sys_user where user_id=?1", nativeQuery = true)
    int deleteWhere(Long userid);

    @Transactional
    @Modifying
    @Query(value = "update sys_user set email=?2 where user_id=?1", nativeQuery = true)
    Integer updateWhere(Long userid, String email);

    //-----------------------------------
    @Transactional
    @Modifying
    @Query(value = "insert into sys_user (user_id,login_name,user_name,email,create_time) values(?1, ?2, ?3, ?4, ?5)", nativeQuery = true)
    int addSave(Long userId, String loginName, String userName, String email, Date createTime);

}